package com.shaoshaohuo.app.ui.ec;

import java.util.ArrayList;
import java.util.List;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.constant.ListViewAction;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.PurchaseRecord;
import com.shaoshaohuo.app.entity.PurchaseRecordEntity;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.adapter.PurchaseRecordListAdapter;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;
import com.shaoshaohuo.app.view.XListView.IXListViewListener;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author: yusonglin
 * @类 说 明: 采购记录
 * @version 1.0
 * @创建时间：2015年8月31日 上午10:19:42
 * 
 */
public class PurchaseRecordActivity extends BaseActivity implements IXListViewListener {

	private TopbarView mTopbarView;
	private XListView mListView;

	private PurchaseRecordListAdapter<PurchaseRecord> mAdapter;
	
	private List<PurchaseRecord> mDataList = new ArrayList<PurchaseRecord>();
	private int size = 10;
	private String action = ListViewAction.NEW;
	private String cursor = "0";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(new TopbarView(this));
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				setContentView(R.layout.activity_purchase_record);
				initView();
				setUpView();
				requestData(true);
			}
		}, 200);
	}


	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
		mListView = (XListView) this.findViewById(R.id.listview_data_list);
	}

	private void setUpView() {
		mTopbarView.setCenterText("采购记录");
		mTopbarView.setLeftView(true, true);

		setListView();
	}

	private void requestData(boolean isLoading) {
		if(isLoading){
			startLoadingDialog();
		}
		RequestService.getInstance().purchasePublishedMyList(this, cursor, size+"", action, PurchaseRecordEntity.class, new RequestListener() {
			
			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if(resultData.isOk()){
					PurchaseRecordEntity entity = (PurchaseRecordEntity) resultData;
					List<PurchaseRecord> list = entity.getData().getList();
					mDataList.addAll(list);
					
					if ((mDataList.size() < size) || (action.equals(ListViewAction.HISTORY) && list.size() < size)) {
//						mListView.setPullLoadEnable(false);
						mListView.endLoadingMore("没有更多了");
					}
					setAdapter(mDataList);
					
				}else{
					showToast(resultData.getMsg());
				}
				onLoad();
			}
			
			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(msg);
				onLoad();
			}
		});
	}

	protected void setAdapter(List<PurchaseRecord> mDataList2) {
		
//		if(mDataList2.isEmpty()){
//			mDataList2.add(new PurchaseRecord());
//			mDataList2.add(new PurchaseRecord());
//			mDataList2.add(new PurchaseRecord());
//			mDataList2.add(new PurchaseRecord());
//			mDataList2.add(new PurchaseRecord());
//			mDataList2.add(new PurchaseRecord());
//			mDataList2.add(new PurchaseRecord());
//			mDataList2.add(new PurchaseRecord());
//			mDataList2.add(new PurchaseRecord());
//			mDataList2.add(new PurchaseRecord());
//		}
		
		if(mAdapter == null){
			mAdapter = new PurchaseRecordListAdapter<PurchaseRecord>(this, mDataList2, false);
			mListView.setAdapter(mAdapter);
		}else{
			mAdapter.setData(mDataList2);
		}
	}

	private void setListView() {
		mListView.setPullLoadEnable(true);
		mListView.setPullRefreshEnable(true);
		mListView.setXListViewListener(this);
		
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				PurchaseRecord order = (PurchaseRecord) parent.getItemAtPosition(position);
				Intent intent = new Intent(PurchaseRecordActivity.this,PurchaseRecordDetailActivity.class);
				intent.putExtra(ExtraName.id, order.getId());
				startActivity(intent);
//				Intent intent = new Intent(PurchaseRecordActivity.this, PurchaseRecordDetailActivity.class);
//				startActivity(intent);
			}
		});
	}
	
	@Override
	public void onRefresh() {
//		if(mDataList.size()>0){
//			cursor = mDataList.get(0).getCursor();
//		}else {
			cursor = "0";
//		}
			mDataList.clear();
		action = ListViewAction.NEW;
		mListView.setPullLoadEnable(true);
		requestData(false);
	}

	@Override
	public void onLoadMore() {
		if(mDataList.size()>0){
			cursor = mDataList.get(mDataList.size()-1).getCursor();
		}else {
			cursor = "0";
		}
		action = ListViewAction.HISTORY;
		requestData(false);
	}

	private void onLoad() {
		mListView.stopRefresh();
		mListView.stopLoadMore();
	}
	
}
